<?php
/**
 * Created by PhpStorm.
 * User: zhao
 * Date: 2018/4/26
 * Time: 10:04
 */

namespace App\Repositories;


use App\Models\Aliuser;
use App\Models\CreditsRecod;
use App\Models\JbUserMcard;
use App\Models\SigninShare;
use App\Models\SigninShareRecord;
use App\Models\UserMcardRecord;
use App\Models\Wxuser;
use App\Models\WxuserMcard;

class UserRepository extends BaseRepository
{
    /**
     * 更新或者创建用户
     * @param array $user_info
     * @param string $auth_type
     * @return object
     */
    public function saveOrUpdateAuthUserInfo(array $user_info,string $auth_type)
    {

        if($auth_type=='Alipay'){
            $user = Aliuser::where('ali_user_id',$user_info['ali_user_id'])->first();
            $db_user = new Aliuser();

        }else{
            $user = Wxuser::where('wx_openid',$user_info['wx_openid'])->first();
            $db_user = new Wxuser();
        }

        if(!empty($user)) {//更新
            $user->update($user_info);
        }else{//创建
            $user = $db_user->create($user_info);
        }

        return $user;
    }

    public function setSessionWxuserMcard()
    {
        $wxuser_mcard = UserMcardRecord::where('table_type',$this->getSeesionUser()->table)
            ->where('table_id',$this->getSeesionUser()->id)
            ->first();

        $wxuser_mcard = $wxuser_mcard->wxuserMcard;

        session(['web_wxuser_mcard'=>$wxuser_mcard]);
    }

    /**
     * 执行跟新用户信息
     * @param $data
     * @return boolean
     */
    public function updateUserInfo($data)
    {
        $res= WxuserMcard::where('id_wxuser_mcard',$this->getSeesionWxuseMcard()->id_wxuser_mcard)->update($data);

        session(['web_wxuser_mcard'=>WxuserMcard::where('id_wxuser_mcard',$this->getSeesionWxuseMcard()->id_wxuser_mcard)->first()]);

        return $res;
    }

    /**
     * 获取打卡规则
     * @return mixed
     */
    public function getFirstSigninShare()
    {
        return SigninShare::where('id_merchant',session('web_merchant')->id_merchant)->first();
    }

    /**
     * 今天是否打卡
     * @return mixed
     */
    public function isPunchClock()
    {
        $is_punch_clock_tody = SigninShareRecord::where('id_wxuser_mcard',$this->getSeesionWxuseMcard()->id_wxuser_mcard)
                                                ->where('created_at','like',now()->toDateString().'%')
                                                ->first();
        return $is_punch_clock_tody;
    }

    /**
     * 获取某个月打卡记录
     * @return mixed
     */
    public function getPunchClockByMonth($ymonth='')
    {
        if(!$ymonth) $ymonth = date('Y-m');
        return SigninShareRecord::where('id_wxuser_mcard',$this->getSeesionWxuseMcard()->id_wxuser_mcard)
                                ->where('created_at','like',$ymonth.'%')
                                ->orderBy('created_at','desc')->get();
    }

    /**
     * 获取昨天是否打卡
     */
    public function isYestodaySignin()
    {
        return SigninShareRecord::where('created_at','like',now()->yesterday()->toDateString().'%')
                                ->where('id_wxuser_mcard',$this->getSeesionWxuseMcard()->id_wxuser_mcard)
                                ->first();
    }

    //获取最近一次打卡记录
    public function getRecentlyClockRecord()
    {
        return SigninShareRecord::where('id_wxuser_mcard',$this->getSeesionWxuseMcard()->id_wxuser_mcard)
                                ->orderBy('created_at','desc')
                                ->first();
    }

    /**
     * 签到获取的总积分
     */
    public function getCreditsByClock()
    {
        return CreditsRecod::where('table_type','signin_share_record')
                                    ->where('id_wxuser_mcard',$this->getSeesionWxuseMcard()->id_wxuser_mcard)
                                    ->sum('value');
    }
    /**
     * 根据打卡规则打卡
     * @param $rule
     */
    public function punchClockByRule($rule)
    {
        //最近一次打卡记录
        $recently_record = $this->getRecentlyClockRecord();

        $record_data['id_wxuser_mcard'] = $this->getSeesionWxuseMcard()->id_wxuser_mcard;
        $record_data['id_signin_share'] = $rule->id_signin_share;
        $record_data['id_merchant'] = session('web_merchant')->id_merchant;
        $record_data['signin_rule'] = json_encode($rule->toArray());

        //总连续打卡次数
        //获取昨天是否打卡
        $yestoday_record = $this->isYestodaySignin();

        if(!empty($yestoday_record)){
            $record_data['continuous_times'] = $yestoday_record->continuous_times+1;
        }

        //一轮连续打卡总计
        $record_data['times'] = 1;
        if(!empty($recently_record) && $recently_record->times<$rule->sign_many && !empty($yestoday_record)){
            $record_data['times'] = $recently_record->times+1;
        }


        return SigninShareRecord::create($record_data);
    }
}